package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.GoodsTypeVo;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 */
@Service
@SuppressWarnings("all")
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Autowired
    private GoodsDao goodsDao;
    /**
     * 修改库存和进货价
     * @param inventoryQuantity
     * @param purchasingPrice
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO saveStock(Integer inventoryQuantity, double purchasingPrice, Integer goodsId) {
        goodsDao.saveStock(inventoryQuantity,purchasingPrice,goodsId);
        return new ServiceVO(100,"100");
    }

    /**
     * 删除库存
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteStock(Integer goodsId) {
        int size = goodsDao.deleteStock(goodsId);
        if (size == 0) {
            return new ServiceVO(ErrorCode.GOODS_TYPE_ERROR_CODE, ErrorCode.GOODS_TYPE_ERROR_MESS);
        } else {
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }
    }

    @Override
    public Map<String, Object> listAlarm() {
        Map<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.listAlarm();
        result.put("rows", list);
        result.put("total", list.size());
        return result;
    }

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for (int i = 4; i > intCode.toString().length(); i--) {

            unitCode = "0" + unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    //获取全部类别
    @Override
    public List<GoodsTypeVo> getGoodstypes() {
        //goodsTypeDao
        List<GoodsType> goodsTypes = goodsTypeDao.getAll();
        Map<Integer, List<GoodsType>> map = goodsTypes.stream().collect(Collectors.groupingBy(GoodsType::getpId));
        goodsTypes.forEach(node -> node.setChildren(map.get(node.getGoodsTypeId())));
        List<GoodsType> collect = goodsTypes.stream().filter(v -> v.getpId().equals(-1)).collect(Collectors.toList());
        List<GoodsTypeVo> result = new ArrayList<>();
        for (int i = 0; i < collect.size(); i++) {
            GoodsTypeVo goodsTypeVo1 = new GoodsTypeVo();
            goodsTypeVo1 = this.change(collect.get(i));
            if (collect.get(i).getChildren().size() > 0) {
                for (int j = 0; j < collect.get(i).getChildren().size(); j++) {
                    GoodsTypeVo goodsTypeVo2 = new GoodsTypeVo();
                    goodsTypeVo2 = this.change(collect.get(i).getChildren().get(j));
                    if (collect.get(i).getChildren().get(j).getChildren().size() > 0) {
                        for (int k = 0; k < collect.get(i).getChildren().get(j).getChildren().size(); k++) {
                            GoodsTypeVo goodsTypeVo3 = new GoodsTypeVo();
                            goodsTypeVo3 = this.change(collect.get(i).getChildren().get(j).getChildren().get(k));
                            goodsTypeVo2.getChildren().add(goodsTypeVo3);
                        }
                    }

                    goodsTypeVo1.getChildren().add(goodsTypeVo2);
                }
            }
            result.add(goodsTypeVo1);
        }
        return result;
    }

    /**
     * 分页查询商品库存信息
     *
     * @param page        当前页
     * @param rows        每页显示条数
     * @param codeOrName  商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @Override
    public Map<String, Object> getlistInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> result = new HashMap<>();
        List<Goods> list = goodsDao.getlistInventory((page - 1) * rows, rows, codeOrName, goodsTypeId);
        result.put("rows", list);
        result.put("total", list.size());
        return result;
    }

    @Override
    public Map<String, Object> getlist(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> list = goodsDao.getList((page - 1) * rows, rows, goodsName, goodsTypeId);
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

    @Override
    public void save(String goodsTypeName, Integer pId) {
        goodsTypeDao.save(goodsTypeName, pId);
    }

    @Override
    public void delete(Integer goodsTypeId) {
        goodsTypeDao.delete(goodsTypeId);
    }

    /**
     * 增加商品
     *
     * @param goods
     */
    @Override
    public void saveOrUpdateGoods(Goods goods) {
        if (StringUtils.isEmpty(goods.getGoodsId())) {
            //增加
            goodsDao.insert(goods);
        } else {
            //修改
            goodsDao.update(goods);
        }
    }

    /**
     * 删除商品并判断条件
     *
     * @param goodsId
     * @return
     */
    @Override
    public ServiceVO deleteGoods(Integer goodsId) {
        int size = goodsDao.delete(goodsId);
        if (size == 0) {
            return new ServiceVO(ErrorCode.GOODS_TYPE_ERROR_CODE, ErrorCode.GOODS_TYPE_ERROR_MESS);
        } else {
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        }
    }

    /**
     * 查询无库存的商品
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> list = goodsDao.getNoInventoryQuantity((page - 1) * rows, rows, nameOrCode);
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

    /**
     * 分页查询有库存商品信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> list = goodsDao.getHasInventoryQuantity((page - 1) * rows, rows, nameOrCode);
        map.put("total", list.size());
        map.put("rows", list);
        return map;
    }

    public GoodsTypeVo change(GoodsType goodsType) {
        GoodsTypeVo goodsTypeVo = new GoodsTypeVo();
        goodsTypeVo.setId(goodsType.getGoodsTypeId());
        goodsTypeVo.setText(goodsType.getGoodsTypeName());
        goodsTypeVo.setIconCls("goods-type");
        goodsTypeVo.setState(goodsType.getGoodsTypeState() == 0 ? "open" : "closed");
        return goodsTypeVo;
    }

}
